/**
 * 
 */
package gpl.scotlandyard.android.controllers;

import gpl.scotlandyard.android.R;
import gpl.scotlandyard.android.activities.SimpleTrackerActivity;
import gpl.scotlandyard.android.activities.SplashActivity;
import gpl.scotlandyard.android.beans.adapters.FugitivePositionList;
import gpl.scotlandyard.android.utils.InputValidator;
import gpl.scotlandyard.beans.MarkedNode;
import gpl.scotlandyard.beans.basics.Ticket;
import gpl.scotlandyard.services.MoveTracker;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.GridView;
import android.widget.Toast;

/** @author norbert */
public class SimpleTrackerController extends BasicController {

  private static final String TAG = SimpleTrackerController.class.toString();

  private static final SimpleTrackerController INSTANCE = new SimpleTrackerController();

  private static final int FIRST_SHOW_TURN = 3;

  private static final int MIN_POSITION = 1;

  private int turn = 0;

  private MoveTracker tracker;

  private SimpleTrackerController() {
  }

  @Override
  public void onViewCreated() {
    tracker = new MoveTracker(getModel().getFugitive());
    tracker.sortByNumber();
    GridView gridview = (GridView) getActivity().findViewById(R.id.simpleTracker_grid);
    gridview.setAdapter(new FugitivePositionList(getActivity(), R.id.simpleTracker_grid, tracker));
    turn = 0;
    refreshView();
  }

  /**
   * @param text
   * @param actionId
   * @param event
   */
  public boolean onPlayerAppears(String text, int actionId, KeyEvent event) {
    int maxNode = getModel().getMap().size();
    int setPosition = InputValidator.isValidPosition(text, MIN_POSITION, maxNode);
    if (setPosition == -1) {
      Log.w(TAG, "Position must be between [" + MIN_POSITION + "," + maxNode + "] : " + text);
      Toast.makeText(getActivity(), "Give position between [" + MIN_POSITION + "," + maxNode + "]", Toast.LENGTH_SHORT).show();
      return true;
    }

    if (turn == 0) {
      turn = FIRST_SHOW_TURN;
    }
    tracker.setKnownPosition(getModel().getMap().get(setPosition - 1));
    refreshView();
    return false;
  }

  /**
   * @param v
   * @param ticket
   */
  public void onClickTicketButton(View v, Ticket ticket) {
    if (turn > 0) {
      turn++;
    }
    tracker.movesWithTicket(ticket);
    refreshView();
  }

  /**
   * 
   */
  public void onClickRollback() {
    if (tracker.rollback()) {
      turn--;
    }
    refreshView();
  }

  /**
   * @param menu
   * @param v
   * @param menuInfo
   */
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    getActivity().getMenuInflater().inflate(R.menu.node_evaluation_cm, menu);
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    MarkedNode markedNode = tracker.getPossiblePositions().get(info.position);
    MenuItem menuItem = null;
    if (markedNode.getMark() == MarkedNode.HOPEFULLY_MARK) {
      menuItem = menu.findItem(R.id.cm_node_evaluation_hopefully);
    } else if (markedNode.getMark() == MarkedNode.HOPELESS_MARK) {
      menuItem = menu.findItem(R.id.cm_node_evaluation_hopeless);
    } else { // if (markedNode.getMark() == MarkedNode.NEUTRAL_MARK)
      menuItem = menu.findItem(R.id.cm_node_evaluation_neutral);
    }
    menuItem.setChecked(true);
  }

  /**
   * @param item
   * @param mark
   */
  public void onNodeMarked(MenuItem item, int mark) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    tracker.changeMark(info.position, mark);
    // if need performance : do 2 methods
    // one for change mark
    // one for delete impossible mark and refresh just on the second
    refreshView();
  }

  /** Refreshes UI view. */
  private void refreshView() {
    boolean taxi = tracker.couldMoveWith(Ticket.TAXI);
    boolean bus = tracker.couldMoveWith(Ticket.BUS);
    boolean ug = tracker.couldMoveWith(Ticket.UG);
    boolean hide = tracker.couldMoveWith(Ticket.HIDE);
    int possibilities = tracker.getPossiblePositions().size();
    boolean canRollback = tracker.getStackSize() > 0;
    getActivity().refreshView(taxi, bus, ug, hide, possibilities, turn, canRollback);
  }

  @Override
  public void onBackPressed() {
    onBackPressed(SplashActivity.class);
  }

  /** @return the instance */
  public static SimpleTrackerController getInstance() {
    return INSTANCE;
  }

  @Override
  public SimpleTrackerActivity getActivity() {
    return (SimpleTrackerActivity) super.getActivity();
  }

  /** Sorts list by number. */
  public void sortListByNumber() {
    tracker.sortByNumber();
  }

  /** Sorts list by mark. */
  public void sortListByMark() {
    tracker.sortByMark();
  }

  public void clearMarks() {
    tracker.clearMarks();
  }

}
